home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / convert / toicc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-14  |  5.7 KB  |  207 lines

  1. /*
  2. %    TOICC . C
  3. %
  4. % Read any images (man ccs) and output a ICC image padded to 8-pixel width.
  5. %
  6. % AUTHOR:    Jin Guojun, LBL - 1991
  7. */
  8.  
  9. #include "header.def"
  10. #include "imagedef.h"
  11. #include <time.h>
  12.  
  13. #ifndef    ICC_HEADER_LEN
  14. #define    ICC_HEADER_LEN    60
  15. #endif
  16.  
  17. arg_fmt_list_string    arg_list[] =    {
  18.     {"-k", "%b", True, 1, 0, "    keep going & ignore error"},
  19.     {"-f", "%b", True, 1, 1, "    enhance on forground"},
  20.     {"-g", "%f", 0., 1, 1, "    gamma for background"},
  21.     {"-l", "%-", 0, 1, 0, "    rotate left"},
  22.     {"-r", "%+", 0, 1, 0, "    rotate right"},
  23.     {"-t", "%s", No, 1, 1, "    title"},
  24.     {"-w", "%b", True, 1, 0, "    without header output"},
  25. {"    [<] input [[ > | ] output]", "0", 0, 0, 0, "End"},    NULL};
  26.  
  27. ICC_HEADER    icc_hd = {ICC_MAGIC, ICC_HEADER_LEN};
  28. U_IMAGE    uimg;
  29. bool    Etype, nostop, rot, wh;
  30. float    gamma;
  31. int    *hist;
  32. char    *obuf[4];
  33.  
  34. #define    rows    uimg.height
  35. #define    cols    uimg.width
  36.  
  37. #ifndef    GAMMA_BASE
  38. #define    GAMMA_BASE    2.49998
  39. #endif
  40.  
  41. main(ac, av)
  42. int    ac;
  43. char*    av[];
  44. {
  45. char    **fl, *title, date_time[8];
  46. int    nf, maxval, new_rows, new_cols, r_size;
  47. long    clock;
  48. MType    x_size, row, cnt;
  49. struct    tm*    date;
  50. pixel**    xel24;
  51.  
  52.     uimg.color_dpy = True;
  53.     format_init(&uimg, IMAGE_INIT_TYPE, COLOR_PS, ICC, *av, "D20-1");
  54.  
  55.     if ((nf=parse_argus(&fl, ac, av, arg_list,
  56.         &nostop, &Etype, &gamma, &rot, &rot, &title, &wh)) < 0)
  57.         exit(nf);
  58.     if (gamma)    {
  59.         if (Etype)
  60.             gamma = -gamma;
  61.         gamma -= GAMMA_BASE;
  62.     }
  63.     if (nf && !freopen(uimg.name=fl[0], "rb", stdin))
  64.         syserr("input file -- %s", fl[0]);
  65.  
  66. io_test(fileno(in_fp), {parse_usage(arg_list); exit(-1);});
  67.  
  68. if ((*uimg.header_handle)(HEADER_READ, &uimg, 0, 0, True) < 0)
  69.     syserr("unknown image type");
  70.  
  71. if (uimg.in_type==PPM || uimg.in_type==PNM)
  72.     xel24 = (pixel**)(*uimg.std_swif)(FI_PNM_MAXVAL, &uimg, &maxval);
  73. (*uimg.std_swif)(FI_LOAD_FILE, &uimg, nostop ? NULL : uimg.name,
  74.     True /* don't change format, and save PNM loading buffer */);
  75.  
  76. clock = time((long *)0);
  77. date  = localtime(&clock);
  78.  
  79. if (rot)
  80.     new_rows = cols,    new_cols = rows;
  81. else    new_rows = rows,    new_cols = cols;
  82.  
  83. r_size = x_size = (new_cols + 7) & 0xFFFFFFF8L;
  84.     /* creation date */
  85.     icc_hd.date = (((long)date->tm_year << 24) & 0xFF000000) +
  86.         (((long)(date->tm_mon + 1) << 16) & 0x00FF0000) +
  87.         (((long)date->tm_mday << 8)    & 0x0000FF00) +
  88.         ((long)date->tm_hour        & 0x000000FF);
  89.     icc_hd.time = (((long)date->tm_min << 24) & 0xFF000000) +
  90.         (((long)date->tm_sec << 16) & 0x00FF0000);
  91.     if (uimg.name)
  92.         strncpy(icc_hd.user_name, uimg.name, 32);
  93.     /* subfile header length */
  94.     icc_hd.img_hd.len = sizeof(icc_hd.img_hd);
  95.         /* subfile length */
  96.     icc_hd.img_hd.length = icc_hd.img_hd.len + (x_size * new_rows);
  97.     icc_hd.img_hd.File_Type = 7;    /* ICC type */
  98.     icc_hd.img_hd.orig_width = new_cols;
  99.     icc_hd.img_hd.x_size = x_size;    /* image new width */
  100.     icc_hd.img_hd.y_size = icc_hd.img_hd.orig_height = new_rows;
  101.     icc_hd.img_hd.gray_scale =
  102.         uimg.color_form==CFM_SGF;    /* color=0, greyscale=1    */
  103.     icc_hd.img_hd.planes =
  104.         icc_hd.img_hd.gray_scale ? 1 : 3;/*    plane count    */
  105.  
  106.     if (!wh)
  107.         fwrite(&icc_hd, sizeof(icc_hd), 1, out_fp);
  108.     msg("image size:   %ld(w) x %ld(h)\n", x_size, new_rows);
  109.     if (rot)
  110.         r_size = cols;
  111.     for (cnt=0; cnt < icc_hd.img_hd.planes; cnt++)
  112.         obuf[cnt] = nzalloc(x_size, new_rows, "icc-obuf");
  113.  
  114.     if (uimg.color_form != CFM_SGF && uimg.color_form != CFM_SEPLANE) {
  115.     register char    *pr = obuf[0],    *pg = obuf[1],    *pb = obuf[2];
  116.  
  117.     switch (uimg.in_type) {
  118.     register int    col;
  119.     case PPM:
  120.     case PNM:
  121.         for (row=0; row < rows; row++)    {
  122.         register pixel    *pP = xel24[row];
  123.             for (col=0; col < cols; col++, pP++)
  124.             pr[col] = PPM_GETR(*pP),
  125.             pg[col] = PPM_GETG(*pP),
  126.             pb[col] = PPM_GETB(*pP);
  127.         pr+=r_size,    pg+=r_size,    pb+=r_size;
  128.         }
  129.         free(xel24);
  130.         break;
  131.     default:
  132.         if (uimg.channels==3)
  133.             if (uimg.color_form == CFM_ILL ||
  134.             uimg.in_type==TiFF && uimg.color_form != CFM_ILC) {
  135.             byte*    scan[3];
  136.                 scan[2] = (byte*)uimg.src - cols;
  137.                 for (row=0; row < rows; row++)    {
  138.                     scan[0] = scan[2] + cols;
  139.                     scan[1] = scan[0] + cols;
  140.                     scan[2] = scan[1] + cols;
  141.                     memcpy(pr, scan[0], cols);
  142.                     memcpy(pg, scan[1], cols);
  143.                     memcpy(pb, scan[2], cols);
  144.                     pr+=r_size, pg+=r_size, pb+=r_size;
  145.                 }
  146.             }
  147.             else {
  148.             register byte*    ibp = (byte*) uimg.src;
  149.             for (row=rows; row--;pr+=r_size, pg+=r_size, pb+=r_size)
  150.                 for (col=0; col < cols; col++)
  151.                 pr[col] = *ibp++,
  152.                 pg[col] = *ibp++,
  153.                 pb[col] = *ibp++;
  154.             }
  155.         else    {
  156.         register byte    *cmap[3], *bp = (byte *)uimg.src;
  157.             cmap[0] = reg_cmap[0];
  158.             cmap[1] = reg_cmap[1];
  159.             cmap[2] = reg_cmap[2];
  160.             if (cmap[0] == NULL)
  161.                 rlemap_to_regmap(cmap, &rle_dflt_hdr);
  162.             for (row=rows; row--; bp+=col, pr+=r_size, pg+=r_size, pb+=r_size)
  163.                 for (col=0; col < cols; col++)
  164.                 pr[col] = cmap[0][bp[col]],
  165.                 pg[col] = cmap[1][bp[col]],
  166.                 pb[col] = cmap[2][bp[col]];
  167.         }
  168.     }
  169.     }
  170.     else while (cnt--)    {
  171.     register char    *po = obuf[cnt], *ps = (char*)uimg.src + cols*rows*cnt;
  172.     for (row=rows; row--; po+=r_size, ps+=cols)
  173.         memcpy(po, ps, cols);
  174.     }
  175.     free(uimg.src);
  176.  
  177.     if (gamma) {
  178.     LKT*    lkt = zalloc(MaxColors, sizeof(*lkt), "icc-lkt");
  179.     hist = nzalloc(MaxColors, sizeof(*hist), "icc-hist");
  180.     row = new_rows * x_size;
  181.     for (cnt=icc_hd.img_hd.planes; cnt--;)
  182.         apply_gamma(obuf[cnt], row, lkt);
  183.     }
  184.     if (rot) {
  185.     cnt = icc_hd.img_hd.planes;
  186.     obuf[cnt] = nzalloc(x_size, new_rows, "icc-rot");
  187.     while (cnt--)
  188.         rotate90(obuf[cnt], obuf[cnt+1], new_rows, x_size, 1, rot+1);
  189.     }
  190.     for (cnt=0, rot = rot!=0; cnt<icc_hd.img_hd.planes; cnt++)
  191.     if (fwrite(obuf[cnt + rot], new_rows, x_size, out_fp) != x_size)
  192.         syserr("write data");
  193.     for (cnt = icc_hd.img_hd.planes + rot; cnt--;)
  194.     free(obuf[cnt]);
  195. exit(0);
  196. }
  197.  
  198. apply_gamma(bp, size, lkt)
  199. register byte*    bp;
  200. register LKT*    lkt;
  201. {
  202. register maxval = histogram_calc(bp, size, hist);
  203.     eta_curve(lkt, gamma, maxval, Etype);
  204.     for (maxval=size; maxval--;)
  205.         bp[maxval] = lkt[bp[maxval]];
  206. }
  207.